# encoding=utf-8
import sys
from time import sleep


class MyClass:
    # 方法的定义
    @staticmethod
    def static_func(*args, **kwargs):
        pass

    @classmethod
    def class_func(cls, *args, **kwargs):
        pass

    def instance_func(self, *args, **kwargs):
        pass

    # 属性定义
    def __init__(self):
        self.property_name = None
        self._name = None

    # setter 与 getter定义
    @property
    def name(self):
        n = None
        if self._name:
            # TODO: add code
            n = self._name

        return n

    @name.setter
    def name(self, n):
        # TODO: add code to validate argument.
        self._name = n


# 协程
# def receiver():
#        sleep(5)
#        yield 123
#
#
# def receiver_2():
#     m = yield from receiver()
#     if m == 123:
#         print("Right")
#     else:
#         print("Wrong")
#
#
# y = receiver_2()
# sleep(10)
# y.send("abc")
# y.send("xyz")
# y.send("mn")


# y = receiver_2()
# y.send(None)
# print(y.send("nihao"))

# websocket
# import asyncio
# import websockets
#
# async def hello(websocket, path):
#     name = await websocket.recv()
#     print(f"< {name}")
#
#     greeting = f"Hello {name}!"
#
#     await websocket.send(greeting)
#     print(f"> {greeting}")
#
# start_server = websockets.serve(hello, 'localhost', 3001)
#
# asyncio.get_event_loop().run_until_complete(start_server)
# asyncio.get_event_loop().run_forever()

from pyim import serializer, server
server.start()
# packet = serializer.serialize({"name": "sonto"})
# print(packet)
# import os
#
# print("This is in parent process.")
# pid = os.fork()
# if pid > 0:
#     print("Now I created a new process with PID: " + str(pid) + "PID:" + str(os.getpid()))
# elif pid == 0:
#     print("This is child process with PID:" + str(os.getpid()) + " and my PPID:" + str(os.getppid()))

from pyim import server

# server.start(daemon=False)

# import os, signal
#
# def func1():
#     print("hello")
#     signal.alarm(1)
#
# def sig_handler(sig, obj):
#     if sig == signal.SIGCHLD:
#         print("The sub process exited aleady")
#     elif sig == signal.SIGALRM:
#         func1()
#     elif sig == signal.SIGKILL:
#         print("Someone is killing me.")
#
# signal.signal(signal.SIGALRM, sig_handler)
# # signal.alarm(2)
# signal.signal(signal.SIGU)
# pid = os.fork()
# if pid > 0:
#     sleep(2)
#     os.kill(pid, signal.SIGKILL)
#     os.kill(os.getpid(), signal.SIGALRM)
# elif pid == 0:
#     print("Sub process will sleep.")
#     sleep(10)
#     print("Sub process is wakeup.")
#     sys.exit(0)

# def func1():
#     raise Exception
#
# class Ex(BaseException):
#     pass
#
# try:
#     func1()
# except Ex:
#     print("Good")


